<template>
   <div class="app-container">
      <div class="p4">
         <FaSearchBar :show-toggle="false">
            <template #default="{ fold, toggle }">
               <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="80px">
                  <el-form-item label="系统模块" prop="title">
                     <el-input v-model="queryParams.title" placeholder="请输入系统模块" clearable style="width: 150px;"
                        @keyup.enter="handleQuery" />
                  </el-form-item>
                  <el-form-item label="操作人员" prop="username">
                     <el-input v-model="queryParams.username" placeholder="请输入操作人员" clearable style="width: 150px;"
                        @keyup.enter="handleQuery" />
                  </el-form-item>
                  <el-form-item label="类型" prop="operType" v-show="!fold">
                     <el-select v-model="queryParams.operType" placeholder="操作类型" clearable style="width: 150px">
                        <el-option v-for="dict in sys_oper_type" :key="dict.value" :label="dict.label"
                           :value="dict.value" />
                     </el-select>
                  </el-form-item>
                  <el-form-item label="状态" prop="status" v-show="!fold">
                     <el-select v-model="queryParams.status" placeholder="状态" clearable style="width: 150px">
                        <el-option v-for="dict in sys_oper_status" :key="dict.value" :label="dict.label"
                           :value="dict.value" />
                     </el-select>
                  </el-form-item>
                  <el-form-item label="操作时间" style="width: 350px" v-show="!fold">
                     <el-date-picker v-model="dateRange" value-format="YYYY-MM-DD HH:mm:ss" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                        :default-time="[new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 1, 1, 23, 59, 59)]"></el-date-picker>
                  </el-form-item>
                  <el-form-item>
                     <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                     <el-button @click="resetQuery">重置</el-button>
                     <el-button link @click="toggle">
                        <template #icon>
                           <FaIcon :name="fold ? 'i-ep:caret-bottom' : 'i-ep:caret-top'" />
                        </template>
                        {{ fold ? '高级查询' : '高级查询' }}
                     </el-button>
                  </el-form-item>
               </el-form>
            </template>
         </FaSearchBar>
         <el-divider border-style="dashed" style="margin:0px 0px 20px 0px" />

         <el-space wrap>
            <el-button type="danger" icon="Delete" :disabled="multiple" @click="handleDelete"
               v-auth="['oper:log$remove']">删除</el-button>
            <el-button type="danger" icon="Delete" @click="handleClean"
               v-auth="['oper:log$remove']">清空</el-button>
            <el-button type="warning" icon="Download" @click="handleExport"
               v.auth="['oper:log$export']">导出</el-button>
         </el-space>
         <el-table ref="operlogRef" v-loading="loading" :data="operlogList" @selection-change="handleSelectionChange"
            :default-sort="defaultSort" @sort-change="handleSortChange" stripe highlight-current-row border class="mt4">
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column label="#" align="center" prop="id" />
            <el-table-column label="模块" align="center" prop="module" width="120" :show-overflow-tooltip="true" />
            <el-table-column label="类型" align="center" prop="type" width="100">
               <template #default="scope">
                  <DictTag :dictData="sys_oper_type" :value="scope.row.type" />
               </template>
            </el-table-column>
            <!-- <el-table-column label="请求方式" align="center" prop="method" /> -->
            <el-table-column label="设备" align="center" prop="device" width="120" />
            <el-table-column label="系统" align="center" prop="os" width="120" />
            <el-table-column label="浏览器" align="center" prop="browser" width="120" />
            <el-table-column label="IP" align="center" prop="ip" width="130" :show-overflow-tooltip="true" />
            <el-table-column label="操作地点" align="center" prop="location" width="100" />
            <el-table-column label="状态" align="center" prop="status" width="100">
               <template #default="scope">
                  <DictTag :dictData="sys_oper_status" :value="scope.row.status" />
               </template>
            </el-table-column>
            <el-table-column label="耗时" align="center" prop="time" width="110" :show-overflow-tooltip="true"
               sortable="custom" :sort-orders="['descending', 'ascending']">
               <template #default="scope">
                  <span>{{ scope.row.time }}毫秒</span>
               </template>
            </el-table-column>
            <el-table-column label="操作人" align="center" prop="username" width="110" :show-overflow-tooltip="true"
               sortable="custom" :sort-orders="['descending', 'ascending']" />
            <el-table-column label="操作日期" align="center" prop="operAt" width="180" sortable="custom"
               :sort-orders="['descending', 'ascending']">
               <template #default="scope">
                  <span>{{ parseTime(scope.row.operAt) }}</span>
               </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="100">
               <template #default="scope">
                  <el-button size="small" plain icon="View" @click="handleView(scope.row, scope.index)"
                     v-auth="['oper:log$query']">详细</el-button>
               </template>
            </el-table-column>
         </el-table>

         <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize" @pagination="getList" />
      </div>
      <!-- 操作日志详细 -->
      <el-dialog title="操作日志详细" v-model="open" width="700px" append-to-body>
         <el-form :model="form" label-width="100px">
            <el-row>
               <el-col :span="12">
                  <el-form-item label="模块：">{{ form.module }}</el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="状态：">
                     <DictTag :dictData="sys_oper_status" :value="form.status" />
                  </el-form-item>
               </el-col>
               <el-col :span="24">
                  <el-form-item label="请求地址：">{{ form.url }}</el-form-item>
               </el-col>
               <el-col :span="24">
                  <el-form-item label="请求方式：">{{ form.method }}</el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="IP：">{{ form.ip }}</el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="操作地点：">{{ form.location }}</el-form-item>
               </el-col>
               <el-col :span="24">
                  <el-form-item label="设备：">{{ form.device }}</el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="系统：">{{ form.os }}</el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="浏览器：">{{ form.browser }}</el-form-item>
               </el-col>
               <el-col :span="24">
                  <el-form-item label="描述：">{{ form.description }}</el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="耗时：">{{ form.time }}毫秒</el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="操作人：">{{ form.username }}</el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="操作时间：">{{ parseTime(form.operAt) }}</el-form-item>
               </el-col>
               <el-col :span="22">
                  <el-form-item label="请求参数：">
                     <el-input v-model="form.payload" type="textarea" :rows="5" :readOnly="true"
                        readonly /></el-form-item>
               </el-col>
               <el-col :span="22">
                  <el-form-item label="返回结果：">
                     <el-input v-model="form.result" type="textarea" :rows="8" :readOnly="true"
                        readonly /></el-form-item>
               </el-col>
               <el-col :span="22">
                  <el-form-item label="异常信息：" v-if="form.status === 1">{{ form.error }}</el-form-item>
               </el-col>
            </el-row>
         </el-form>
         <template #footer>
            <div class="dialog-footer">
               <el-button @click="open = false">关 闭</el-button>
            </div>
         </template>
      </el-dialog>
   </div>
</template>

<script setup name="Operlog">
import apiOperLog from "@/api/monitor/operlog";

const { proxy } = getCurrentInstance();
const { sys_oper_type, sys_oper_status } = proxy.useDict("sys_oper_type", "sys_oper_status");

const operlogList = ref([]);
const open = ref(false);
const loading = ref(false);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);
const defaultSort = ref({ prop: "operAt", order: "descending" });

const data = reactive({
   form: {},
   queryParams: {
      pageNum: 1,
      pageSize: 10,
      title: undefined,
      username: undefined,
      operType: undefined,
      status: undefined
   }
});

const { queryParams, form } = toRefs(data);

/** 查询登录日志 */
function getList() {
   // loading.value = true;
   apiOperLog.page(proxy.addDateRange(queryParams.value, dateRange.value)).then(response => {
      operlogList.value = response.data.rows;
      total.value = response.data.total;
   }).finally(() => {
    loading.value = false
  })
}
/** 操作日志类型字典翻译 */
function typeFormat(row, column) {
   return proxy.selectDictLabel(sys_oper_type.value, row.operType);
}
/** 搜索按钮操作 */
function handleQuery() {
   queryParams.value.pageNum = 1;
   getList();
}
/** 重置按钮操作 */
function resetQuery() {
   dateRange.value = [];
   proxy.resetForm("queryRef");
   queryParams.value.pageNum = 1;
   proxy.$refs["operlogRef"].sort(defaultSort.value.prop, defaultSort.value.order);
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
   ids.value = selection.map(item => item.id);
   multiple.value = !selection.length;
}
/** 排序触发事件 */
function handleSortChange(column, prop, order) {
   queryParams.value.orderByColumn = column.prop;
   queryParams.value.isAsc = column.order;
   getList();
}
/** 详细按钮操作 */
function handleView(row) {
   open.value = true;
   form.value = row;
}
/** 删除按钮操作 */
function handleDelete(row) {
   const operIds = row.id || ids.value;
   proxy.$modal.confirm('是否确认删除日志编号为"' + operIds + '"的数据项?').then(function () {
      return apiOperLog.delete(operIds);
   }).then(() => {
      getList();
      proxy.$modal.success("删除成功");
   }).catch(() => { });
}
/** 清空按钮操作 */
function handleClean() {
   proxy.$modal.confirm("是否确认清空所有操作日志数据项?").then(function () {
      return apiOperLog.clean();
   }).then(() => {
      getList();
      proxy.$modal.success("清空成功");
   }).catch(() => { });
}
/** 导出按钮操作 */
function handleExport() {
   apiOperLog.download(queryParams.value, "操作日志");
}

getList();
</script>
